﻿//  Copyright (c) 2011 Ray Liang (http://www.dotnetage.com)
//  Licensed MIT: http://www.opensource.org/licenses/mit-license.php

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web;

namespace DNA.Mvc.Notification
{
    public class Notifier
    {
        private static INotificationService Service
        {
            get 
            {
                return DependencyResolver.Current.GetService<INotificationService>();
            }
        }

        public static bool IsSubscribed(string token)
        {
            if ((HttpContext.Current != null) && (HttpContext.Current.Request.IsAuthenticated) && (HttpContext.Current.User.Identity != null))
                return IsSubscribed(token, HttpContext.Current.User.Identity.Name);
            return false;
        }

        public static bool IsSubscribed(string token, string userName)
        {
            return Service.IsSubscribed(token, userName);
        }

        public static bool Subscribe(string token)
        {
            if ((HttpContext.Current != null) && (HttpContext.Current.Request.IsAuthenticated) && (HttpContext.Current.User.Identity != null))
                return Subscribe(token, HttpContext.Current.User.Identity.Name);
            return false;
        }

        public static bool Subscribe(string token, string userName)
        {
            if (!IsSubscribed(token, userName))
                return Service.Subscribe(token, userName);
            return true;
        }

        public static void Unsubscribe(string token)
        {
            if ((HttpContext.Current != null) && (HttpContext.Current.Request.IsAuthenticated) && (HttpContext.Current.User.Identity != null))
                Unsubscribe(token, HttpContext.Current.User.Identity.Name);
        }

        public static void Unsubscribe(string token, string userName)
        {
            if (IsSubscribed(token, userName))
                Service.Unsubscribe(token, userName);
        }

        public static void Send(string token, IMessage message)
        {
            Service.Send(token, message);
        }

        public static void Send(string token, string title, string content, string contentType = "text/html", string encoding = "utf-8", string type = "")
        {
            Service.Send(token, title, content, contentType, encoding, type);
        }
    }
}
